package com.example.mine_platform.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.example.mine_platform.exception.CustomException;
import com.example.mine_platform.pojo.dto.UserLoginDTO;
import com.example.mine_platform.pojo.dto.UserRegisterDTO;
import com.example.mine_platform.pojo.entities.User;
import com.example.mine_platform.mapper.UserMapper;
import com.example.mine_platform.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service
@Slf4j
public class UserServiceImpl implements UserService
{
    @Resource
    private UserMapper userMapper;
    private static final String PASSWORD_REGEX = "^(?:(?=.*[a-z])(?=.*[A-Z])|(?=.*[a-z])(?=.*\\d)|(?=.*[A-Z])(?=.*\\d)|(?=.*[a-z])(?=.*[@#$%^&+=!])|(?=.*[A-Z])(?=.*[@#$%^&+=!])|(?=.*\\d)(?=.*[@#$%^&+=!]))[a-zA-Z0-9@#$%^&+=!]{8,}$";

    private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
    @Override
    public List<User> findAll()
    {
        return userMapper.selectAll();
    }

    @Override
    public User login(@NonNull UserLoginDTO userLoginDTO) {
        String phone = userLoginDTO.getPhone();
        String password = userLoginDTO.getPassword();
        User user = userMapper.getByPhone(phone);
        if(user == null){
            throw new CustomException("用户不存在");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(user.getPassword())){
            //密码错误
            throw new CustomException("账号或密码错误");
        }
        return user;
    }
    @Override
    public void register(UserRegisterDTO userRegisterDTO) {
        String phone = userRegisterDTO.getPhone();
        String password = userRegisterDTO.getPassword();
        String username = userRegisterDTO.getUserName();
        Boolean sex=userRegisterDTO.getSex();
        int count = userMapper.existUser(phone);
        if(count >= 1){
            log.info("用户已存在，请使用未注册过的phone注册");
            throw new CustomException("用户已存在，请使用未注册过的phone注册");
        }
        Pattern phonePattern = Pattern.compile(PHONE_REGEX);
        Matcher phoneMatcher = phonePattern.matcher(phone);
        if (!phoneMatcher.matches()) {
            log.info("手机号格式错误");
            throw new CustomException("手机号格式错误");
        }

        Pattern passwordPattern = Pattern.compile(PASSWORD_REGEX);
        Matcher passwordMatcher = passwordPattern.matcher(password);
        if (!passwordMatcher.matches()) {
            log.info("密码格式错误");
            throw new CustomException("密码格式错误");
        }

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        User user = User.builder()
                .phone(phone)
                .password(password)
                .userName(username)
                .sex(sex)
                .level("User")
                .deleted((byte) 0)
                .build();
        userMapper.insertSelective(user);

    }
    @Override
    public int update(User user)
    {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User getById(Integer id)
    {
        return userMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<User> findBySearch(String userName, Integer pageSize, Integer pageNum){
        //分页
        PageHelper.startPage(pageNum, pageSize);
        //接下来的查询会自动开启分页查询

        List<User> userList = userMapper.getUserList(userName);
        if(ObjectUtil.isEmpty(userList)){
            return PageInfo.of(new ArrayList<>());
        }
        return PageInfo.of(userList);
    }
}
